#!/usr/bin/env python

#   Trolls Outta Luckland v1.1.0
#   Copyright 2009-2010, Gummbum
#
#   This file is part of Trolls Outta Luckland.
#
#   Trolls Outta Luckland is free software: you can redistribute it
#   and/or modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation, either version 3 of
#   the License, or (at your option) any later version.
#
#   Trolls Outta Luckland is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Trolls Outta Luckland.  If not, see
#   <http://www.gnu.org/licenses/>.

__version__ = '1.1.0'

import pygame
from pygame.locals import Color, RLEACCEL

import game_globals
from game_globals import *
from ui import color as ui_color
from utils import get_font

class Fps(pygame.sprite.Sprite):
    """Sprite for rendering frames-per-second"""
    def __init__(self, surface, xy=(0,0), bg=None, fg=(255,255,255),
        milli=2000, font_name='default'):
        """Create a Fps renderer"""
        pygame.sprite.Sprite.__init__(self)
        self.surface = surface
        self.font = get_font(font_name)
        self.fg = fg
        self.bg = bg
        self.default_bg = ui_color('transparent')
        self.milli = milli
        self.image = self.font.render('00000 fps', 0, fg, fg)
        self.rect = self.image.get_rect()
        self.rect.topleft = xy
        self.clock = pygame.time.Clock()
        self.fps = 1
        self.elapsed = milli
    def update(self, *args):
        if self.elapsed >= self.milli:
            self.fps = self.clock.get_fps()
            if self.fps == 0:
                self.fps = 1
            fps = '%.0f fps' % self.fps
            if self.bg is None:
                self.image = self.font.render(fps, 1, self.fg, self.default_bg)
                self.image.set_colorkey(self.default_bg, RLEACCEL)
            else:
                self.image = self.font.render(fps, 1, self.fg, self.bg)
                self.image.set_colorkey(None)
            self.image.set_alpha(208)
            self.elapsed = 0
    def tick(self, framerate=0):
        """Call once per frame. See pygame.time.Clock."""
        fps = self.clock.tick(framerate)
        self.elapsed += fps
        return fps
    def get_time(self):
        return self.clock.get_time()
    def set_timer(self, milli):
        """Change update interval"""
        self.milli = milli
    def benchmark(self, duration=1000):
        clock = pygame.time.Clock()
        surf = pygame.display.get_surface()
#        rect = surf.get_rect()
        copy = pygame.surface.Surface((WINDOW_WIDTH,WINDOW_HEIGHT)).convert()
        copy.fill((1,1,1))
        copy.set_colorkey((1,1,1))
        rect = game_globals.game.window.rect
        elapsed = 0
        blit = surf.blit
        flip = pygame.display.flip
        tick = clock.tick
        while elapsed < duration:
            blit(copy, rect)
            flip()
            elapsed += tick(0)
        self.fps = clock.get_fps()
#        print 'elapsed=%d' % elapsed
#        print 'fps=%.2f' % self.fps
        return self.fps

if __name__ == '__main__':
    pass
